import java.util.List;

import  org.openqa.selenium.NoSuchElementException;

import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class BasfFirefoxDriver extends FirefoxDriver{

	private int timeOutLength = 10000;
	
	private static Proxy proxy = new Proxy().setProxyAutoconfigUrl("http://webproxy.global.basf.net/proxy.pac");
	private static FirefoxProfile profile = new FirefoxProfile().setProxyPreferences(proxy);
	public BasfFirefoxDriver()
	{
		super(profile);
		
	}
	public WebElement findElement(By by)
	{
		WebElement searchedElement = null;
		long timeOut = System.currentTimeMillis() + timeOutLength;
		while (searchedElement == null && timeOut > System.currentTimeMillis())
		{
			try
			{
				searchedElement = super.findElement(by);
			}
			catch (NoSuchElementException e)
			{
				if (timeOut < System.currentTimeMillis())
					throw new NoSuchElementException("Could find the searched Element");
			}
		}
		return searchedElement;
	}
	public List<WebElement> findElements(By by)
	{
		List<WebElement> list = super.findElements(by);
		long timeOut = System.currentTimeMillis() + timeOutLength;
		//No Try, Catch in the loop, because findElements does not throw exceptions
		while (list.size() < 1 && timeOut > System.currentTimeMillis())
		{
			list = super.findElements(by);
		}
		//Added Exception throwing for findElements(By by)
		if (list.size() < 1)
			throw new NoSuchElementException("Could find the searched Elements");
		else
			return list;
	}
}
